<?php
/**
 *
 * @author iegss
 *        
 */
class mobileapi_rpc_indexad {
	
	/**
	 */
	function __construct($app) {
		$this->app = $app;
	}
	
	function get_all_list() {
		
		$db = kernel::database();
		
		/*
		$sql = "select *  FROM `sdb_mobileapi_indexad` where disabled = 'false' order by group_code asc, ordernum asc;";
		$indexads = $db->select($sql);
		
		$groupad = array();
		foreach ($indexads as $key => $v){
			
			$v['ad_img'] = base_storager::image_path($v['ad_img']);
			$groupad[$v['group_code']]['group_code'] = $v['group_code'];
			$groupad[$v['group_code']]['group_name'] = $v['group_name'];
			$groupad[$v['group_code']]['items'][] = $v;
		}
		
		$res = array();
		foreach ($groupad as $key => $value) {
			$res[] = $value;
		}
		*/
		
		$sql = "select *  FROM `sdb_mobileapi_indexad_group` where disabled = 'false' order by ordernum asc, group_id asc;";
		$indexads_group = $db->select($sql);
		$now_time = time();
		foreach ($indexads_group as $key => $v){
			$sql = "select *  FROM `sdb_mobileapi_indexad` where disabled = 'false' and group_id = '".$v['group_id']."' order by ordernum asc, ad_id asc;";
			$indexads = $db->select($sql);
			$groupad = array();
			foreach ($indexads as $ads){
				if ($ads['url_type'] == 'goods') {
					$sql = 'select g.buy_count, sg.initial_num FROM `sdb_starbuy_special_goods` AS sg LEFT JOIN `sdb_b2c_products` AS p ON p.product_id = sg.product_id LEFT JOIN `sdb_b2c_goods` AS g ON p.goods_id = g.goods_id WHERE sg.product_id = '.$ads['ad_url'];
					$counts = $db->selectrow($sql);
					$ads['buy_count'] = $counts['buy_count'] + $counts['initial_num'];
				}
				$ads['ad_img'] = base_storager::image_path($ads['ad_img']);
				$groupad[] = $ads;
			}
			$indexads_group[$key]['items'] = $groupad;
			$indexads_group[$key]['system_time'] = $now_time; 
		}
		
		return $indexads_group;
		
		//print_r($res);exit;
	}
        
        function get_local_ad(){
          
            $local_id=$_POST['local_id'];
            $group_code=$local_id;
           
            $db = kernel::database();
            $sql="select *  FROM sdb_mobileapi_indexad where disabled = 'false' and group_id = '". $group_code."' order by ordernum asc, ad_id asc;";
            
            $local_ad=$db->select($sql);
            foreach($local_ad as $ad){
                $ad['ad_img'] = base_storager::image_path($ad['ad_img']);
                $adlist[]=$ad;
            }
            if($adlist){
                return $adlist;
            }else{
                return '广告不存在';
            }
            
        }
        
        // function get_ad(){
        //     $app_ad_id = $_REQUEST['app_ad_id'];
        //     $ad = kernel::database()->select(sprintf("select * from sdb_mobileapi_indexad_group where app_ad_key = ".$app_ad_id." and showtype = 'app_ad' and disabled=false order by group_id "));
        //     return $ad;
        // }
          function get_ad($filter){
        	
            $app_ad_key=$filter['app_ad_key'];
            $res = app::get('mobileapi')->model('indexad_group')->getList('group_id',array('app_ad_key'=>$app_ad_key)); 
            $group_id=$res[0]['group_id'];
            $local_ad = app::get('mobileapi')->model('indexad')->getList("*",array('group_id'=>$group_id),0,-1,"ordernum asc");
//            $db = kernel::database();
//            //$sql="select *  FROM sdb_mobileapi_indexad where disabled = 'false' and group_id = '". $group_id."' order by ordernum asc, ad_id asc;";
//            $sql = "select i.* FROM ".kernel::database()->prefix."mobileapi_indexad_group g left join ".kernel::database()->prefix."mobileapi_indexad i ON i.group_id = g.group_id "
//                    . "where g.app_ad_key = '".$group_id."' and i.disabled = true and g.disabled = true";
//            $local_ad=$db->selectlimit($sql);
            foreach($local_ad as $ad){
                $ad['ad_img'] = base_storager::image_path($ad['ad_img']);
                $adlist[]=$ad;
            }
            if($adlist){
                return $adlist;
            }else{
                return '广告不存在';
            }
        }
        /**
        * 根据条形码获取商品
        * 2016年10月10日 13:38:44
        **/
        function get_barcode($filter){
        	if (!$filter['barcode'])
	        {
	        	return '缺少必要参数！';
	        }
            $app_barcode=$filter['barcode'];

	        $default_product = app::get('b2c')->model('products')->getList('product_id',array('barcode'=>$app_barcode,'is_default'=>'true','marketable'=>'true'));
	        if(!$default_product){
	        	return '商品不存在';
	        }
         	// $goods['url'] = app::get('site')->router()->gen_url(array('app'=>'b2c','ctl'=>'site_product','full'=>1,'act'=>'index','arg'=>$default_product[0]['product_id']));


	        return $default_product[0]['product_id'];
        }

}

?>